package com.fulu.game.bi.service.impl.room;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.fulu.game.bi.common.util.csv.CsvExportUtil;
import com.fulu.game.bi.common.util.csv.Mapper;
import com.fulu.game.bi.common.util.csv.Querier;
import com.fulu.game.bi.entity.bo.channel.VoiceGuildRoomListBO;
import com.fulu.game.bi.entity.dto.VoiceGuildRoomListDTO;
import com.fulu.game.bi.entity.enums.QueryTypeStatusEnum;
import com.fulu.game.bi.entity.vo.room.VoiceGuildRoomConsumerDetailLineVO;
import com.fulu.game.bi.entity.vo.room.VoiceGuildRoomConsumerDetailVO;
import com.fulu.game.bi.hbase.HBaseRoomStatAllUserDAO;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
@Slf4j
public class VoiceGuildRoomConsumerDetailService extends BaseVoiceService {
    @Autowired
    private HBaseRoomStatAllUserDAO hBaseRoomStatAllUserDAO;

    public PageInfo<VoiceGuildRoomConsumerDetailVO> list(VoiceGuildRoomListDTO voiceGuildRoomListDTO) {
        VoiceGuildRoomListBO voiceGuildRoomListBO = new VoiceGuildRoomListBO();
        BeanUtil.copyProperties(voiceGuildRoomListDTO, voiceGuildRoomListBO);
        voiceGuildRoomListBO.setTableName(tableNameMap.get(voiceGuildRoomListDTO.getQueryType()));
        voiceGuildRoomListBO.setDataFromStr(voiceGuildRoomListDTO.getDataFrom() == null ? null : voiceGuildRoomListDTO.getDataFrom() + "");
        voiceGuildRoomListBO.setRoomCategoryIdStr(voiceGuildRoomListDTO.getRoomCategoryId() == null ? null : voiceGuildRoomListDTO.getRoomCategoryId() + "");
        voiceGuildRoomListBO.setPageNo((voiceGuildRoomListDTO.getPageNum() - 1) * voiceGuildRoomListDTO.getPageSize());
        List<VoiceGuildRoomConsumerDetailVO> list = hBaseRoomStatAllUserDAO.guildRoomConsumerDetailList(voiceGuildRoomListBO);
        Integer count = hBaseRoomStatAllUserDAO.countForGuildRoomConsumerDetailList(voiceGuildRoomListBO);
        PageInfo<VoiceGuildRoomConsumerDetailVO> pageInfo = new PageInfo<>(list);
        pageInfo.setTotal(count == null ? 0 : count);
        List<VoiceGuildRoomConsumerDetailVO> voiceGuilds = pageInfo.getList().stream().map(map -> {
            QueryTypeStatusEnum anEnum = QueryTypeStatusEnum.getEnum(voiceGuildRoomListDTO.getQueryType());
            switch (anEnum) {
                case DAY:
                    map.setStartDate(map.getStatDate());
                    map.setEndDate(map.getStatDate());
                    break;
                case WEEK:
                    map.setStartDate(map.getStatDate());
                    map.setEndDate(DateUtil.formatDate(DateUtil.offsetDay(DateUtil.parse(map.getStatDate()), 6)));
                    break;
                case MONTH:
                    map.setStartDate(map.getStatDate() + "-01");
                    map.setEndDate(DateUtil.formatDate(DateUtil.offsetDay(DateUtil.offsetMonth(DateUtil.parse(map.getStatDate() + "-01"), 1), -1)));
                    break;
                default:
                    break;
            }
            map.setGuildCode(Objects.equals(map.getGuildCode(), "0") ? null : map.getGuildCode());
            return map;
        }).collect(Collectors.toList());
        PageInfo<VoiceGuildRoomConsumerDetailVO> voPageInfo = new PageInfo<>();
        BeanUtils.copyProperties(pageInfo, voPageInfo);
        voPageInfo.setList(voiceGuilds);
        return voPageInfo;
    }

    public void export(VoiceGuildRoomListDTO dto) {
        String fileName = "语言互动-公会房间";
        LinkedHashMap<String, Mapper<VoiceGuildRoomConsumerDetailVO>> map = new LinkedHashMap<>();
        QueryTypeStatusEnum anEnum = QueryTypeStatusEnum.getEnum(dto.getQueryType());
        switch (anEnum) {
            case DAY:
                map.put("日期", v -> v.getStartDate());
                break;
            default:
                map.put("日期", v -> v.getStartDate() + "-" + v.getEndDate());
                break;
        }
        map.put("房间ID", v -> v.getRoomNo());
        map.put("房间名称", v -> v.getRoomName());
        map.put("房间类型", v -> v.getRoomCategory());
        map.put("公会ID", v -> v.getGuildCode());
        map.put("公会名称", v -> v.getGuildName());
        map.put("钻石消费", v -> v.getVirtualMoneyAll().toString());
        map.put("消费人数", v -> v.getConsumeUserNum() + "");
        map.put("消费次数", v -> v.getConsumeNum() + "");
        map.put("魅力值流水", v -> v.getCharmMoney().toString());
        map.put("购买礼物人数", v -> v.getBuyGiftUserNum() + "");
        map.put("购买礼物次数", v -> v.getBuyGiftNum() + "");
        map.put("购买礼物流水", v -> v.getBuyGiftMoneyAll() + "");
        map.put("礼盒礼物人数", v -> v.getBuyBoxUserNum() + "");
        map.put("礼盒礼物次数", v -> v.getBuyBoxNum() + "");
        map.put("礼盒礼物流水", v -> v.getBuyBoxMoneyAll() + "");
        map.put("红包礼物人数", v -> v.getBuyRedbagUserNum() + "");
        map.put("红包礼物次数", v -> v.getBuyRedbagNum() + "");
        map.put("红包礼物流水", v -> v.getBuyRedbagMoneyAll() + "");
        map.put("背包礼物赠送人数", v -> v.getBagGiftUserNum() + "");
        map.put("背包礼物赠送次数", v -> v.getBagGiftNum() + "");
        map.put("背包礼物魅力值流水", v -> v.getBagGiftMoneyAll() + "");
        map.put("购买守护人数", v -> v.getBuyGuardUserNum() + "");
        map.put("购买守护次数", v -> v.getBuyGuardNum() + "");
        map.put("守护魅力值流水", v -> v.getBuyGuardMoneyAll() + "");
        Querier querier = (num) -> list(dto);
        CsvExportUtil.export(100, fileName, map, querier);
    }

    public List<VoiceGuildRoomConsumerDetailLineVO> listLine(VoiceGuildRoomListDTO voiceGuildRoomListDTO) {
        VoiceGuildRoomListBO voiceGuildRoomListBO = new VoiceGuildRoomListBO();
        BeanUtil.copyProperties(voiceGuildRoomListDTO, voiceGuildRoomListBO);
        voiceGuildRoomListBO.setTableName(tableNameMap.get(voiceGuildRoomListDTO.getQueryType()));
        voiceGuildRoomListBO.setDataFromStr(voiceGuildRoomListDTO.getDataFrom() == null ? null : voiceGuildRoomListDTO.getDataFrom() + "");
        voiceGuildRoomListBO.setRoomCategoryIdStr(voiceGuildRoomListDTO.getRoomCategoryId() == null ? null : voiceGuildRoomListDTO.getRoomCategoryId() + "");
        List<VoiceGuildRoomConsumerDetailLineVO> list = hBaseRoomStatAllUserDAO.guildRoomConsumerDetailLineList(voiceGuildRoomListBO);
        return list.stream().map(map -> {
            map.setQueryType(voiceGuildRoomListDTO.getQueryType());
            return map;
        }).collect(Collectors.toList());
    }
}
